const DB=wx.cloud.database().collection("list")
var currentAccountId
var currentAccountLiked=[]
var app = getApp()
// var currentAccountLiked1=[]
let refreshtimes=1
Page({
  /**
   * 生命周期函数--监听页面加载
   */
toAdd:function(){
  wx.navigateTo({
    url: '/pages/forum/addQue/addQue',
  })
},
toLike:function(){
  wx.navigateTo({
    url: '/pages/forum/MyLike/MyLike?='+JSON.stringify(app.globalData.currentAccountId),
  })
},
toMyQues:function(){
  wx.navigateTo({
    url: '/pages/forum/MyQues/MyQues',
  })
},
AddCom:function(){
  wx.navigateTo({
    url: '/pages/forum/addCom/addCom',
  })
},
onPostTap:function(event){
  var id=event.currentTarget.dataset.detailid;
  var questions=this.data.questions
  var temp
  var likes
  for(var i=0;i<questions.length;i++)
  {
    if(questions[i]._id===id)
    {
      likes=questions[i].likes
      if(questions[i].liked===true)
      {
        likes--
        temp=false
        questions[i].liked=false
        questions[i].likes=likes
        // var value=currentAccountLiked.findIndex(function(value,index,arr){
        //   return value=id
        // })
        // currentAccountLiked.splice(value,1)
        this.setData({
          questions:questions
        })
          wx.showToast({
            title: '已取消收藏！',
          })
      }
      else{
        likes++
        temp=true
        questions[i].liked=temp
        questions[i].likes=likes
        // currentAccountLiked.push(id)
        this.setData({
          questions:questions
        })
        wx.showToast({
          title: '已收藏！',
        })
      }
      break
    }
  }
  DB.doc(id).update({
              data:{
                // liked:temp,
                likes:likes
              }
            })
var currentAccountLiked1=[]
for(var i=0;i<questions.length;i++){
if(questions[i].liked===true){
  currentAccountLiked1.push(questions[i]._id)
}
}
console.log(currentAccountLiked1)
let db=wx.cloud.database().collection("User").doc(currentAccountId).update({
  data:{
    likequestions:currentAccountLiked1
  }
})
},

  onReady: function () {
    // console.log("onShow当前用户id,",app.globalData.currentAccountId)
    // var questions=[]
    // DB.get().then(res=>{
    //   var res1=[]
    //   for(let i=0;i<res.data.length;i++)
    //   {
    //     res1[i]=res.data[i]
    //     res1[i].liked=false
    //   }
    //   this.setData({questions:res1})
    //   questions=res1
    //   })
    // let db1=wx.cloud.database() 
    //   currentAccountId=app.globalData.currentAccountId
    //   console.log(currentAccountId)
    //   db1.collection("User").get().then(res3=>{
    //     for(var i=0;i<res3.data.length;i++){
    //       if(currentAccountId===res3.data[i]._id){
    //         currentAccountLiked=res3.data[i].likequestions
    //         for(var j=0;j<questions.length;j++){
    //           for(var k=0;k<currentAccountLiked.length;k++){
    //             if(questions[j]._id===currentAccountLiked[k]){
    //               questions[j].liked=true
    //             }
    //           }
    //         }
    //         this.setData({questions:questions})
    //         break
    //       }
    //     }
    //   })
    
  },
  toSearch:function(){
    wx.navigateTo({
      url: '/pages/forum/Search/Search',
    })
    // console.log(currentAccountId)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onLoad:function(options){
    console.log("onload的当前用户id",app.globalData.currentAccountId)
    var questions=[]
    DB.get().then(res=>{
      var res1=[]
      for(let i=0;i<res.data.length;i++)
      {
        res1[i]=res.data[i]
        res1[i].liked=false
      }
      // this.setData({questions:res1})
      questions=res1
      let db1=wx.cloud.database() 
      currentAccountId=app.globalData.currentAccountId
      console.log(currentAccountId)
      db1.collection("User").get().then(res3=>{
        for(var i=0;i<res3.data.length;i++){
          if(currentAccountId===res3.data[i]._id){
            currentAccountLiked=res3.data[i].likequestions
            for(var j=0;j<questions.length;j++){
              for(var k=0;k<currentAccountLiked.length;k++){
                if(questions[j]._id===currentAccountLiked[k]){
                  questions[j].liked=true
                }
              }
            }
            this.setData({questions:questions})
            break
          }
        }
      })

      })
 
    
  },
  onShow: function () {
    // console.log("onShow当前用户id,",app.globalData.currentAccountId)
    var _this = this;
    let aShow = '';
    //获取存储信息
    wx.getStorage({
      key: 'aShow',
      success: function (res) {
        // success
        aShow = res.data;
        if (aShow) {
        // 获取数据函数
          _this.onLoad()
        }
      }
    })

    var questions=[]
    DB.get().then(res=>{
      var res1=[]
      for(let i=0;i<res.data.length;i++)
      {
        res1[i]=res.data[i]
        res1[i].liked=false
      }
      // this.setData({questions:res1})
      questions=res1
      let db1=wx.cloud.database() 
      currentAccountId=app.globalData.currentAccountId
      console.log(currentAccountId)
      db1.collection("User").get().then(res3=>{
        for(var i=0;i<res3.data.length;i++){
          if(currentAccountId===res3.data[i]._id){
            currentAccountLiked=res3.data[i].likequestions
            for(var j=0;j<questions.length;j++){
              for(var k=0;k<currentAccountLiked.length;k++){
                if(questions[j]._id===currentAccountLiked[k]){
                  questions[j].liked=true
                }
              }
            }
            this.setData({questions:questions})
            break
          }
        }
      })
      })
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showLoading({
      title: '玩命加载中',
      mask:true
    })
    setTimeout(() => {
      wx.hideLoading();
      }, 500)
      // console.log(this.data.questions.length)
    var questions=this.data.questions
    DB.skip(this.data.questions.length).get().then(res=>{
      var res1=[]
      for(let i=0;i<res.data.length;i++)
      {
        res1[i]=res.data[i]
        res1[i].liked=false
      }
      // this.setData({questions:res1})
      // questions=res1
      for(var i=0;i<res1.length;i++){
        questions.push(res1[i])
      }
      console.log(questions)
      // console.log(res1)
      let db1=wx.cloud.database() 
      currentAccountId=app.globalData.currentAccountId
      console.log(currentAccountId)
      db1.collection("User").get().then(res3=>{
        for(var i=0;i<res3.data.length;i++){
          if(currentAccountId===res3.data[i]._id){
            currentAccountLiked=res3.data[i].likequestions
            for(var j=0;j<questions.length;j++){
              for(var k=0;k<currentAccountLiked.length;k++){
                if(questions[j]._id===currentAccountLiked[k]){
                  questions[j].liked=true
                }
              }
            }
            this.setData({questions:questions})
            break
          }
        }
      })
      })
    
  // wx.hideLoading();
  },

  /**

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  toComment:function(e) {
    let Q_id=e.currentTarget.dataset.id
    console.log(Q_id)
    wx.navigateTo({
      url: "/pages/forum/addCom/addCom?Q_id="+JSON.stringify(Q_id),
    })
  },
})